#include <iostream>
#include <algorithm>
using namespace std;
/*
 * @lc app=leetcode.cn id=67 lang=cpp
 *
 * [67] 二进制求和
 */

// @lc code=start
class Solution {
public:
    string addBinary(string a, string b) {
        int count = 0; string res;
        int i = a.size() - 1, j = b.size() - 1;
        while (count > 0 || i >= 0 || j >= 0) {
            int num = 0;
            if (i >= 0) num += a[i] - '0';
            if (j >= 0) num += b[j] - '0';
            num += count; count = num / 2;
            res.push_back(num % 2 + '0');
            i--; j--;
        }
        reverse(res.begin(), res.end());
        return res;
    }
};
// @lc code=end

